<?php
include ($_SERVER['DOCUMENT_ROOT'].'/insp-modules/misc/config.php');

$link = mysqli_connect($host, $user, $password, "eventfeedback");

if (mysqli_connect_errno()) {
	printf("Connect Error!");
	exit();
}

session_start();
$login_json = $_SESSION['login_json'];
$login_info = json_decode($login_json, true);
$username = $login_info['username'];
$pwd = $login_info['password'];

$query = "CALL Login('$username', '$pwd')";
if (mysqli_multi_query($link, $query)) {
	if ($result_login = mysqli_use_result($link)) {
		while ($row = mysqli_fetch_assoc($result_login)) {
			$user_info_json = json_encode($row);
			$_SESSION['user_info_json'] = $user_info_json;
			$_SESSION['isLogin'] = 1;
			// echo print_r($user_info_json);
		}
	}
	if (mysqli_next_result($link)) {
		mysqli_free_result($result_login);
	}
}

$query = "CALL GetChapters()";
if (mysqli_multi_query($link, $query)) {
	if ($result_chapter = mysqli_use_result($link)) {
		$chapter_name = array();
		while ($row = mysqli_fetch_assoc($result_chapter)) {
			$name = $row['name'];
			array_push($chapter_name, $name);
		}
		$chapter_name_json = json_encode($chapter_name);
		$_SESSION['chapter_name_json'] = $chapter_name_json;
	}
	if (mysqli_next_result($link)) {
		mysqli_free_result($result_chapter);
	}
}

$query = "CALL GetEventDetails()";
if (mysqli_multi_query($link, $query)) {
	if ($result_program = mysqli_use_result($link)) {
		$program_name = array();
		while ($row = mysqli_fetch_assoc($result_program)) {
			array_push($program_name, $row);
		}
		$program_name_json = json_encode($program_name);
		$_SESSION['program_name_json'] = $program_name_json;
	}
	if (mysqli_next_result($link)) {
		mysqli_free_result($result_program);
	}
}

mysqli_close($link);

// echo print_r($_SESSION);
header ('location: home.php');
?>